<?php
namespace Home\Controller;
use Home\Common\Controller\CommonController;
class IndexController extends CommonController {
    public function index(){
        //查找所有的友链数据
        $links=M('links')->select();
        $this->assign('links',$links);
//        dd($links);
       //搜索
        if(IS_POST){
//            dd($_POST);die;
            $gname=htmlspecialchars($_POST['gname']);
//            dd($gname);die;
            (new SearchController())->searchs($gname);die;
        }
        //查找顶级分类,及其一级分类，机器子类
        $category=M('category')->where("sid=0")->select();
        foreach($category as $k=>$v){
            //一级分类
            $cid=$v['cid'];
            $category[$k]['firstCategory']=M('category')->where("sid=$cid")->select();
             // 一级分类的所有子类
            foreach($category[$k]['firstCategory'] as $m=>$n){
//                dd($n['cid']);
              $sons=$this->finson($n['cid'],M('category')->select(),1);
                //解决子级为空时报错
                    if(empty($sons)){
                        $map['cid']  = array('in',[0]);
                    }else{
                        $map['cid']  = array('in',$sons);
                    }
                    $category[$k]['firstCategory'][$m]['sonscategory']=M('category')->where($map)->select();
            }
        }
//        die;
        $this->assign('category',$category);
//        dd($category);
        //轮播图数据
        $banner=M('banner')->select();
        $this->assign('banner',$banner);
        //轮播return数据
        $stick=M('goods')->where("status=1")->order('sort desc')->limit(2)->select();
        $this->assign('stick',$stick);
        //轮播图下面的的数据
        $recom=M('goods')->where('recom=1 AND status=1')->order('gid desc')->limit(5)->select();
        $this->assign('recom',$recom);
        //热门活动数据
        $hot=M('goods')->where('hot=1 AND status=1')->order('gid desc')->limit(6)->select();
        $this->assign('hot',$hot);
//        dd($hot);
        //楼层一,手机
        $floor1=$this->floor1();
        $this->assign('floor1',$floor1);
//        dd($floor1);
//        dd($floor1[1]);
//        楼层2，智能穿戴
        $floor2=$this->floor2();
        $this->assign('floor2',$floor2);
        //新品速递
        $news=$this->news();
        $this->assign('news',$news);
        //找出购物车中的商品数量
        $cars=$this->cars();
//        购物车中数量为空时
        if(!$cars['cars']){
            $cars['cars']=0;
        }
        $this->assign('cars',$cars['cars']);
//        dd($cars['cars']);
        $this->display();
    }
    /**
     * 楼层1数据
     * 手机
     *
     */
    private  function floor1(){
        $sons=$this->finson(2,M('category')->select());
        $map['cid']  = array('in',$sons);
//        dd($map['cid']);
        //热门
        $one=M('goods')->where($map)->where("recom=1 AND status=1")->order('sort desc')->limit(2)->select();
        //推荐商品
      $recom=M('goods')->where($map)->where("hot=1 AND status=1")->order('sort desc')->limit(6)->select();
      //热销推荐
       $hot1=M('goods')->where($map)->where("status=1")->order('sort desc')->limit(4)->select();
       $hot2=M('goods')->where($map)->where("status=1")->order('sort desc')->limit(5,4)->select();
        return [$one,$recom,$hot1,$hot2];
    }
    /**
     * 楼层2数据
     * 智能穿戴
     */
    private  function floor2(){
        $sons=$this->finson(9,M('category')->select());
//        dd($sons);
        $map['cid']  = array('in',$sons);
//        dd($map['cid']);
        //热门
        $one=M('goods')->where($map)->where("recom=1 AND status=1")->order('sort desc')->limit(2)->select();
        //推荐商品
        $recom=M('goods')->where($map)->where("hot=1 AND status=1")->order('sort desc')->limit(6)->select();
//        dd($recom);
        //热销推荐
        $hot1=M('goods')->where($map)->where("status=1")->order('sort desc')->limit(4)->select();
        $hot2=M('goods')->where($map)->where("status=1")->order('sort desc')->limit(5,4)->select();
        return [$one,$recom,$hot1,$hot2];
    }


    /**
     * 楼层3数据
     */
    /**
     * 楼层4数据
     */
    /**
     * 新品速递
     */
    private function news(){
        return M('goods')->where("status=1")->order('addtime desc')->limit(15)->select();
    }

    /**
     * 购物车中商品的数量
     */
    public  function  cars(){
        if(isset($_SESSION['uid'])){
            $uid=session('uid');
            return M('usergoods')->field('sum(count) cars')->where("uid={$uid} AND gstatus=0")->find();
        }else{
            return ['cars'=>0];
        }
    }



}

